<%--
  Created by IntelliJ IDEA.
  User: Rui
  Date: 2018/9/15
  Time: 13:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="net.expectx.gw.bean.SystemSetting" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String imgSrc=basePath+"resources/images/noImage.jpg";
    session.setAttribute("imgSrc",imgSrc);
    Object num = session.getAttribute("num");
    Object attr=session.getAttribute("attrClass");
    Byte attrClass = 0;
    if (num != null) {
        if (num.equals("1")) {
            session.removeAttribute("attrClass");
            session.removeAttribute("system");
        } else if (num.equals("0")) {
            session.setAttribute("num", "1");
            if(session.getAttribute("attrClass")!=null){
            attrClass = Byte.parseByte(session.getAttribute("attrClass").toString());}
        }
    }

%>
<!DOCTYPE html>
<html>
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
    <style>
        .layui-card-body label{width: 115px}
        .layui-card-body .layui-input-block{margin-left: 145px}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card-body">

        <div class="layui-tab" lay-filter="systemTab">
            <ul class="layui-tab-title">
                <c:forEach var="systemType" items="${systemTypes}">
                    <li class="layui-this"  lay-id="${systemType.typeCode}">${systemType.typeName}</li>
                </c:forEach>
            </ul>
            <div class="layui-tab-content layui-card">
                <c:forEach var="systemType" items="${systemTypes}">
                <div class="layui-tab-item    ${systemType.typeCode==0?'layui-show':''}">
                    <form class="layui-form" action="">
                            <c:forEach var="systemSetting" items="${systemSetting}">
                                <c:if test="${systemSetting.attrClass==systemType.typeCode}">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"><a href="##" class="tool"
                                                                           attr_id="${systemSetting.id}">${systemSetting.attrName}</a></label>
                                        <c:if test="${systemSetting.attrType==0}">
                                            <div class="layui-input-inline" style="width:60%">
                                                <input type="text" name="${systemSetting.id}"
                                                       value="${systemSetting.attrValue}" lay-verify="title"
                                                       autocomplete="off" placeholder="请输入" class="layui-input">
                                            </div>
                                            <div class="layui-form-mid layui-word-aux">${systemSetting.attrDesc}</div>
                                        </c:if>

                                        <c:if test="${systemSetting.attrType==3}">
                                            <div class="layui-input-inline"style="width:60%">
                                            <textarea placeholder="请输入内容" value="${systemSetting.attrValue}"
                                                      name="${systemSetting.id}" class="layui-textarea">${systemSetting.attrValue}</textarea>
                                            </div>
                                            <div class="layui-form-mid layui-word-aux">${systemSetting.attrDesc}</div>
                                        </c:if>
                                        <c:if test="${systemSetting.attrType==4}">
                                            <div class="layui-input-inline">

                                                <div class="layui-upload">
                                                    <input type="hidden" value="${systemSetting.attrValue==null?imgSrc:systemSetting.attrValue}" name="${systemSetting.id}" id="avatar" lay-verify="required" class="layui-input">
                                                    <div class="layui-upload-list">
                                                        <img class="layui-upload-img" width="100px" src="${systemSetting.attrValue==null?imgSrc:systemSetting.attrValue}" id="avatarImg">
                                                    </div>
                                                    <button type="button" class="layui-btn" id="avatarBth"><i class="layui-icon layui-icon-upload"></i>上传图片</button>

                                                </div>
                                            </div>
                                        </c:if>
                                    </div>

                                </c:if>
                            </c:forEach>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="add-value">立即提交</button>
                            </div>
                        </div>
                    </form>
                </div>
                </c:forEach>
            </div>
        </div>

        <script type="text/html" id="gmtModifiedTpl">
            {{#
            var date = new Date();
            date.setTime(d.gmtModified);
            return date.Format("yyyy-MM-dd hh:mm:ss");
            }}
        </script>


    </div>
</div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'element', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , element = layui.element
            , upload = layui.upload;

        var avatarUploaderInst = upload.render({
            elem: '#avatarBth'
            , url: '<%=basePath%>/manager/file/upload.action?catalog=image&fileType=image&sizeLimit=1&oss=alioss'
            , before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#avatarImg').attr('src', result);
                });
            }
            , done: function (res) {
                //如果上传失败
                if(!res.success){
                    return layer.msg(res.message);
                }else{
                    $("#avatar").val(res.url);
                    return layer.msg('上传成功');
                }
            }
            , error: function () {
                layer.msg("上传失败!");
            }
        });
        var attrClass =<%=attrClass%>;

            element.tabChange('systemTab',attrClass);
        var fn1;
        //删除监听
        $(document).on("click", ".deleteValue", function () {
            var id = $(this).attr("attr_id");

            layer.load(2);

            $.ajax({
                type: "GET",
                url: "<%=basePath%>manager/system/systemSetting/delete.action",
                data: {"id": id},
                success: function (data) {
                    layer.closeAll("loading");
                    data = JSON.parse(data);
                    if (data.flag) {
                        layer.msg("删除成功,正在刷新");
                        setTimeout(function () {
                            window.location.reload()
                        }, 1000);
                    } else {
                        layer.msg(data.msg);
                    }

                }
            });
        });

        //修改监听
        $(document).on("click", ".updateValue", function () {
            var id = $(this).attr("attr_id");
            layer.load(2);
            $.ajax({
                    type: "GET",
                    url: "<%=basePath%>manager/system/systemSetting/update/" + id + ".action",
                    success: function (data) {
                        layer.closeAll("loading");
                        window.location.reload();
                    },
                }
            );


        })
        //监听添加
        form.on('submit(Type-add)', function (data) {

            var field = data.field;
            var id = field.id;

            if (id == "") {
                layer.load(2);
                $.ajax({
                    url: "<%=basePath%>manager/system/systemSetting/create.action",
                    type: "POST",
                    data: field,
                    dataType: "JSON",
                    success: function (data) {

                        layer.closeAll("loading");
                        if (data.success) {
                            layer.msg("添加成功");
                            setTimeout(function () {
                                window.location.reload()
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }
                    }
                });
            }
            //监听获取数据后的修改
            else {
                layer.load(2);
                $.ajax({
                    type: "POST",
                    url: "<%=basePath%>manager/system/systemSetting/update.action",
                    data: field,
                    dataType: "JSON",
                    success: function (data) {
                        console.log(data);

                        layer.closeAll("loading");
                        if (data.success) {
                            layer.msg("修改成功,正在刷新");
                            setTimeout(function () {
                                window.location.reload()
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }

                    }
                });
            }


        });
        //设置value值
        form.on('submit(add-value)', function (data) {
            var field = data.field;
            layer.load(2);
            $.ajax({
                type: "POST",
                url: "<%=basePath%>manager/system/systemSetting/createValue.action",
                data: {"systemSettingValue": JSON.stringify(field)},
                success: function (data) {

                    data = JSON.parse(data);
                    layer.closeAll("loading");
                    if (data.success) {
                        layer.msg("添加成功,正在刷新");
                        setTimeout(function () {
                            window.location.reload()
                        }, 2000);

                    } else {
                        layer.msg(data.message);
                    }

                }
            });
            return false;
        });


    });

    function edit(e) {

    }

</script>
</body>
</html>

